﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using System.Xml.Linq;
using DVK.SP2007.UniversalClient;
using DVK.SP2007.Constants;

namespace DVK.SP2007
{
    /// <summary>
    /// Maps IDvkMessage to SPListItem. This happens, when SPListItem for Inbox list is created.
    /// </summary>
    public class IncomingMessageToListitemMapper : IMessageMapper<SPListItem>
    {
        private IDvkMessage _message;
        private SPListItem _listItem;
        private IDvkResourceManager _resourceManager;

        public IncomingMessageToListitemMapper(IDvkMessage incomingMessage, SPListItem newInboxItem, IDvkResourceManager resourceManager)
        {
            this._message = incomingMessage;
            this._listItem = newInboxItem;
            this._resourceManager = resourceManager;
        }

        #region IMessageConverter<SPListItem> Members

        /// <summary>
        /// Maps metadata from somewhere into the object instance of type T.
        /// <remarks>
        /// Normally this means mapping information from different location into one format (object).
        /// e.g. to IDvkMessage or Inbox SPListItem.
        /// </remarks>
        /// </summary>
        /// <returns></returns>
        public SPListItem Map()
        {
            _listItem[Fields.DvkId.Name] = _message.DhlId;
            _listItem[Fields.UkMessageId.Name] = _message.Id;
            _listItem[Fields.Fault.Name] = _message.Fault != null ? _message.Fault.ToString() : null;
            _listItem[Fields.SenderOrganisationCode.Name] = _message.Sender.Organisation.Code;
            _listItem[Fields.SenderOrganisationName.Name] = _message.Sender.Organisation.Name;
            _listItem[Fields.SenderPersonCode.Name] = _message.Sender.Person.Code;
            _listItem[Fields.SenderPersonName.Name] = _message.Sender.Person.Name;
            _listItem[Fields.CaseName.Name] = _message.CaseName;
            _listItem[Fields.RecipientOrganisationName.Name] = _message.Recipient.Organisation.Name;
            _listItem[Fields.RecipientOrganisationCode.Name] = _message.Recipient.Organisation.Code;
            _listItem[Fields.RecipientPersonCode.Name] = _message.Recipient.Person.Code;
            _listItem[Fields.RecipientPersonName.Name] = _message.Recipient.Person.Name;

            _listItem[Fields.RecipientStatus.Name] = (int)_message.RecipientStatus;
            _listItem[Fields.SendingStatus.Name] = (int)_message.SendingStatus;
            _listItem[Fields.SendingStatusText.Name] = _resourceManager.GetResourceBasedOnEnumElement<DvkSendingStatus>(_message.SendingStatus);
            _listItem[Fields.RecipientStatusText.Name] = _resourceManager.GetResourceBasedOnEnumElement<DvkRecipientStatus>(_message.RecipientStatus);
            _listItem[Fields.SendingDate.Name] = _message.SendingDate;
            _listItem[Fields.ReceivedDate.Name] = _message.ReceivedDate;
            _listItem[Fields.DvkMetaXml.Name] = _message.DataXml.MetaXml.ToString();
            _listItem[Fields.DvkMetainfoXml.Name] = _message.DataXml.Metainfo.ToString();
            _listItem[Fields.DvkTransportXml.Name] = _message.DataXml.Transport.ToString();
            _listItem[Fields.UnitId.Name] = _message.UnitId;
            _listItem[Fields.Title.Name] = _message.Title;
            return _listItem;
        }

        #endregion
    }
}